package net.wds.ssbus.modules.dbconfig.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wds.ssbus.common.utils.PageUtils;
import net.wds.ssbus.common.utils.Query;
import net.wds.ssbus.modules.dbconfig.dao.TbDbModelDao;
import net.wds.ssbus.modules.dbconfig.entity.TbDbModelColumnEntity;
import net.wds.ssbus.modules.dbconfig.entity.TbDbModelEntity;
import net.wds.ssbus.modules.dbconfig.po.DbSourceModelPo;
import net.wds.ssbus.modules.dbconfig.service.TbDbModelColumnService;
import net.wds.ssbus.modules.dbconfig.service.TbDbModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;


@Service("tbDbModelService")
public class TbDbModelServiceImpl extends ServiceImpl<TbDbModelDao, TbDbModelEntity> implements TbDbModelService {

    @Autowired
    TbDbModelService tbDbModelService;

    @Autowired
    TbDbModelColumnService tbDbModelColumnService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbDbModelEntity> page = this.page(
                new Query<TbDbModelEntity>().getPage(params),
                new QueryWrapper<TbDbModelEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveDtil(DbSourceModelPo dbSourceModelPo) {
        TbDbModelEntity tbDbModelEntity = new TbDbModelEntity();
        BeanUtils.copyProperties(dbSourceModelPo,tbDbModelEntity);
        this.save(tbDbModelEntity);
        List<TbDbModelColumnEntity> columnList =dbSourceModelPo.getModelColumnList();
        List<TbDbModelColumnEntity> collect = columnList.stream().map(item -> {
            item.setModelColumn(tbDbModelEntity.getId());
            return item;
        }).collect(Collectors.toList());
        tbDbModelColumnService.saveBatch(collect);
    }

    @Transactional
    @Override
    public void updateByIdDtil(DbSourceModelPo dbSourceModelPo) {
        TbDbModelEntity tbDbModelEntity = new TbDbModelEntity();
        BeanUtils.copyProperties(dbSourceModelPo,tbDbModelEntity);
        this.updateById(tbDbModelEntity);
        List<TbDbModelColumnEntity> modelColumnList = dbSourceModelPo.getModelColumnList();
        ArrayList<TbDbModelColumnEntity> addList = new ArrayList<>();
        ArrayList<TbDbModelColumnEntity> updateList = new ArrayList<>();
        modelColumnList.stream().forEach(item ->{
            if (item.getModelColumn() == null || item.getModelColumn() == 0){
                item.setModelColumn(dbSourceModelPo.getId());
                addList.add(item);
            }
            else {

                updateList.add(item);
            }
        });
        tbDbModelColumnService.updateBatchById(updateList);
        tbDbModelColumnService.saveBatch(addList);
    }

    @Override
    public DbSourceModelPo getByIddtil(Integer id) {
        DbSourceModelPo dbSourceModelPo = new DbSourceModelPo();
        TbDbModelEntity tbDbModelEntity = tbDbModelService.getById(id);
        BeanUtils.copyProperties(tbDbModelEntity,dbSourceModelPo);
        List<TbDbModelColumnEntity> modelColumnlist = tbDbModelColumnService.list(new QueryWrapper<TbDbModelColumnEntity>().eq("model_column", dbSourceModelPo.getId()));
        dbSourceModelPo.setModelColumnList(modelColumnlist);
        return dbSourceModelPo;
    }
}